﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using Wedo.Utility;
using Wedo.Recognition.Domain;
namespace Wedo.Recognition.Repository
{
    public class EFUnitWork : IUnitWork
    {
        private EFDbContext _dbContext;

        #region 基本实现

        public EFUnitWork(EFDbContext dbContext)
        {
            dbContext.CheckNull();
            _dbContext = dbContext; 
        }

        public IRepository<T> Set<T>() where T : class
        {
            return new GenericRepository<T>(_dbContext);
        }

        public CachedRepository<T> CacheSet<T>() where T : class
        {
            return new CachedRepository<T>(_dbContext);
        }

        public int ExSql(string sql, params object[] param)
        {
            return _dbContext.Database.ExecuteSqlCommand(sql, param);
        }

        public IEnumerable<T> SqlQuery<T>(string sql, params object[] param)
        {
            return _dbContext.Database.SqlQuery<T>(sql, param);
        }

        /// <summary>
        /// 执行操作
        /// </summary>
        /// <returns></returns>
        public int SaveChanges()
        {
            return _dbContext.SaveChanges();
        }

        #endregion


        public IUserRepository User { get { return new UserRepository(_dbContext); } }

        public IRoleRepository Role { get { return new RoleRepository(_dbContext); } }


        public void Dispose()
        {
            _dbContext.Dispose();
        }


        //public ICacheRepository<Wedo.Domain.Models.RoleUserInfoes> RoleUserInfoes
        //{ get { return new RoleUserInfoesRepository(_dbContext); } }


        //public ICacheRepository<Wedo.Domain.Models.ResourceRoles> RoleResource
        //{ get { return new RoleResourceRepository(_dbContext); } }
         
    }
}
